package com.ndd.controller;

import com.ndd.constant.Constants;
import com.ndd.dto.*;
import com.ndd.result.R;
import com.ndd.service.UserService;
import com.ndd.utils.JwtUtils;
import com.ndd.vo.UserLoginVO;
import com.ndd.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @ProjectName: cmall-ybu
 * @Titile: UserController
 * @Author: Lucky
 * @Description: 用户控制器
 */
@RestController //整个控制器返回的都是数据
@RequestMapping("/api/user") // 访问的url的上级或者上两级目录
@Slf4j //日志的优先级默认是info
public class UserController {
    @Autowired
    private UserService userService;

    //删除购物车
    @PostMapping("deletecart")
    public R<Void> deletCart(@RequestBody @Validated deletCartDTO deletcartDTO,HttpServletRequest request) {
        //加入购物车明确用户身份：知道谁在加入购物车
        String token = request.getHeader(Constants.TOKEN_HEADER);
        //获取用户身份:登录的用户身份
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        userService.deletCart(deletcartDTO, userLoginVO);
        return R.ok();
    }

    /**
     * 用户注册功能
     *
     * @param registerDTO
     * @return
     */
    @PostMapping("reg")
    public R<Void> register(@RequestBody @Validated RegisterDTO registerDTO) {
        log.debug("获取到的注册信息：{}", registerDTO);
        userService.register(registerDTO);
        return R.ok();
    }

    /**
     * 用户登录模块
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("login")
    public R<UserLoginVO> login(@Validated @RequestBody UserLoginDTO userLoginDTO) {
        UserLoginVO userLoginVO = userService.login(userLoginDTO);
        //登录成功了
        return R.ok(userLoginVO);
    }


    /**
     * 修改用户信息
     *
     * @param userUpdateDTO
     * @return
     */
    @PutMapping("/update")
    public R<String> updateUser(@RequestBody UserUpdateDTO userUpdateDTO, HttpServletRequest httpRequest) {
        //获得用户请求头信息
        String token = httpRequest.getHeader(Constants.TOKEN_HEADER);
        System.out.println(token);
        //获得当前登录操作的用户信息
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        userService.updateUser(userUpdateDTO, userLoginVO);
        return R.ok(Constants.UPDATE_USER);
    }

    /**
     * 修改密码
     *
     * @return
     */
    @PutMapping("updatePwd")
    public R<Void> updatePwd(@RequestBody @Validated UserUpdatePwdDTO userUpdatePwdDTO, HttpServletRequest request) {
        //修改密码是要明确用户身份：知道是谁在修改密码
        String token = request.getHeader(Constants.TOKEN_HEADER);
        //获取用户身份:登录的用户身份
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        //调用业务层处理密码修改的业务
        userService.updatePwd(userUpdatePwdDTO, userLoginVO);
        return R.ok();
    }

    /**
     * 上传头像
     *
     * @param avatar
     * @param request
     * @return
     */
    @PutMapping("avatar")
    public R<Void> upload(MultipartFile avatar, HttpServletRequest request) {
        //获取token
        String token = request.getHeader(Constants.TOKEN_HEADER);
        //获取登录用户
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        //调用业务层
        userService.upload(avatar, userLoginVO);
        return R.ok();
    }

    /**
     * 根据id查询用户信息
     */
    @GetMapping("/id")
    public R<UserVO> selectById(@RequestParam Integer id) {
        UserVO userVO = userService.selectById(id);
        return R.ok(userVO);
    }


}
